var threeSum = function (nums) {
  let sortNum = nums.sort((a, b) => a - b);
  let len = sortNum.length;
  let result = [];
  console.log(sortNum);
  for (let i = 0; i < len; i++) {
    let left = i + 1;
    let right = len - 1;
    while (left < right) {
      if (sortNum[i] + sortNum[left] + sortNum[right] === 0) {
        result.push([sortNum[i], sortNum[left], sortNum[right]]);
        break;
      } else if (sortNum[i] + sortNum[left + 1] + sortNum[right] < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
};

const comp = (value, target) => {
  for (let item of target) {
    if (item.toString() === `${value}`) return true;
  }
  return false;
};
// console.log(threeSum([-1, 0, 1, 2, -1, -4]), comp([0, 0, 0], [[0, 0, 0]]));

const fn = (arr) => {
  arr.sort((a, b) => a - b);
  let len = arr.length;
  let res = [];

  for (let i = 0; i < len; i++) {
    let left = i + 1;
    let right = len - 1;
    let cur = arr[i];
    if (i > 0 && cur === arr[i - 1]) {
      continue;
    }
    while (left < right) {
      if (cur + arr[left] + arr[right] === 0) {
        res.push([cur, arr[left], arr[right]]);
        left++;
      } else if (cur + arr[left] + arr[right] < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return res;
};

console.log(fn([-1, 0, 1, 2, -1, 4]));
